package com.wenwei.paper.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.wenwei.paper.R;
import com.wenwei.paper.base.BaseActivity;
import com.wenwei.paper.base.Constant;
import com.wenwei.paper.bean.UserLoginBean;
import com.wenwei.paper.net.ApiManager;
import com.wenwei.paper.net.BaseSubscriber;
import com.wenwei.paper.utils.AccountValidatorUtil;
import com.wenwei.paper.utils.RogerSPUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/7/13.
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.input_phone)
    EditText inputPhone;
    @Bind(R.id.input_pw)
    EditText inputPw;
    @Bind(R.id.user_login)
    TextView userLogin;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        super.initView();
        StatusBarUtil.setTranslucent(this, 30);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.user_login, R.id.user_register,R.id.user_forget})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_login:
                if(checkInfo()){
                    userLogin();
                }
                break;
            case R.id.user_register:
                Bundle b1 = new Bundle();
                b1.putInt("type",1);
                goActivity(mContext,RegisterActivity.class,b1);
                break;
            case R.id.user_forget:
                Bundle b2 = new Bundle();
                b2.putInt("type",2);
                goActivity(mContext,RegisterActivity.class,b2);
                break;
        }
    }


    BaseSubscriber<UserLoginBean> loginSubscriber;
    private void userLogin(){
        loginSubscriber = new BaseSubscriber<UserLoginBean>(mContext,true,"登录中...") {
            @Override
            public void onSuccess(UserLoginBean userLoginBean) {
                RogerSPUtils.putObject(mContext, Constant.SP_USER_LOGIN_RESULT,userLoginBean);
                Toast.makeText(mContext,"登录成功，立即体验吧",Toast.LENGTH_SHORT).show();
                goActivity(mContext,HostActivity.class);
                finish();
            }
        };
        ApiManager.getInstance().userLogin(loginSubscriber,inputPhone.getText().toString(),inputPw.getText().toString(), JPushInterface.getRegistrationID(mContext));

    }

    private boolean checkInfo(){
        if(inputPhone.getText().toString().equals("") || inputPw.getText().toString().equals("")
                || !AccountValidatorUtil.isMobile(inputPhone.getText().toString())){
            showToast("请检查用户名或密码的正确性...");
            return false;
        }else {
            return true;
        }
    }
}
